package com.atguigu.gulimall.product.service.impl;

import com.atguigu.gulimall.product.entity.Category;
import com.atguigu.gulimall.product.entity.CategoryBrandRelationEntity;
import com.atguigu.gulimall.product.entity.PmsBrand;
import com.atguigu.gulimall.product.dao.CategoryBrandRelationMapper;
import com.atguigu.gulimall.product.service.CategoryBrandRelationService;
import com.atguigu.gulimall.product.service.ICategoryService;
import com.atguigu.gulimall.product.service.IPmsBrandService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("categoryBrandRelationService")
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationMapper, CategoryBrandRelationEntity> implements CategoryBrandRelationService {

    @Autowired
    private ICategoryService categoryService;
    @Autowired
    private IPmsBrandService brandService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryBrandRelationEntity> page = this.page(
                new Query<CategoryBrandRelationEntity>().getPage(params),
                new QueryWrapper<CategoryBrandRelationEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public Boolean saveDetail(CategoryBrandRelationEntity categoryBrandRelation) {
        Category category = categoryService.getById(categoryBrandRelation.getCatelogId());
        PmsBrand brand = brandService.getById(categoryBrandRelation.getBrandId());
        categoryBrandRelation.setBrandName(brand.getName());
        categoryBrandRelation.setCatelogName(category.getName());
        return  this.save(categoryBrandRelation);

    }

    @Override
    public Boolean updateBrandName(Long brandId, String name) {
        CategoryBrandRelationEntity relationEntity = new CategoryBrandRelationEntity();
        relationEntity.setBrandId(brandId);
        relationEntity.setBrandName(name);
        UpdateWrapper<CategoryBrandRelationEntity> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("brand_id",brandId);
        return  this.update(relationEntity,updateWrapper);

    }

    @Override
    public Boolean updateCategoryName(Long catId, String name) {
        CategoryBrandRelationEntity relationEntity = new CategoryBrandRelationEntity();
        relationEntity.setCatelogId(catId);
        relationEntity.setCatelogName(name);
        UpdateWrapper<CategoryBrandRelationEntity> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("catelog_id",catId);

        return  this.update(relationEntity,updateWrapper);
    }

	@Override
	public List<PmsBrand> getBrandsByCatId(Long catId) {
        List<CategoryBrandRelationEntity> relationEntityList = this.baseMapper.selectList(new QueryWrapper<CategoryBrandRelationEntity>()
                .eq("catelog_id", catId));

        List<PmsBrand> brandList = relationEntityList.stream().map(item -> {
            Long brandId = item.getBrandId();
            PmsBrand brandEntity = brandService.getById(brandId);
            return brandEntity;
        }).collect(Collectors.toList());
        return brandList;
    }

}